package com.wslds.blog.action.admin;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.BaseAction;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.FileUtility;
import com.wslds.base.util.Strings;
import com.wslds.bean.Img;
import com.wslds.bean.Imgtype;
import com.wslds.blog.service.ImgService;

@SuppressWarnings("serial")
@Component("imgAdminAction")
@Scope("prototype")
public class ImgAction extends BaseAction {

	private Img img;
	private List<File> file;
	private List<String> fileFileName;
	private String path;
	
	@Resource
	private ImgService imgService;
	
	
	public String listImg(){
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}
		return SUCCESS;
	}
	
	public String listAjaxImg(){
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}
		PageAdapter<Img> adapter = imgService.page("img", img, getPage());
		this.put("adapter", adapter);
		return SUCCESS;
	}
	
	public String insert(){
		
		try{
			Imgtype imgtype = new Imgtype();
			imgtype.setId(img.getImgTypeId());
			
			imgtype = imgService.findOne("imgtype",imgtype );
			
			for(int i=0; i<file.size(); i++){
				String fileName = fileFileName.get(i);
				String randomName = FileUtility.generateRandomFileName(fileName, 5);
				
				File destFile = new File(FileUtility.getPhotoDir(
						getCatalogBaseDir(), imgtype.getLoc(), randomName));
				FileUtils.forceMkdir(destFile.getParentFile()); 
				FileUtils.copyFile(file.get(i), destFile);
				
				if( Strings.isNull(img.getShowName())){
					img.setShowName(fileName);
				}
				
				img.setName(randomName);
				img.setWaterName(FileUtility.getWaterPrefixName(randomName));
				img.setThinName(FileUtility.getThinPrefixName(randomName));
				img.setCreateDate(new Date());
				img.setStat(BlogCons.COMMON_STATE.MEN);
				imgService.save("img", img);
				
				FileUtility.genWaterMark(destFile, getAppBaseDir());
			}
		}catch( Exception e ){
			e.printStackTrace();
			return ERROR;
		}finally{
			
		}
		return SUCCESS;
		
	}
	
	
	public String findThinImg(){
		try{
			Img img_ = imgService.findOne("img", img);
			Imgtype imgtype = new Imgtype();
			imgtype.setId(img_.getImgTypeId());
			imgtype = imgService.findOne("imgtype",imgtype );
			path = FileUtility.getPhotoDir(imgtype.getLoc(), img_.getThinName());
		}catch( Exception e ){
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String findWaterImg(){
		try{
			Img img_ = imgService.findOne("img", img);
			Imgtype imgtype = new Imgtype();
			imgtype.setId(img_.getImgTypeId());
			imgtype = imgService.findOne("imgtype",imgtype );
			path = FileUtility.getPhotoDir(imgtype.getLoc(), img_.getWaterName());
		}catch( Exception e ){
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String findSrcImg(){
		try{
			Img img_ = imgService.findOne("img", img);
			Imgtype imgtype = new Imgtype();
			imgtype.setId(img_.getImgTypeId());
			imgtype = imgService.findOne("imgtype",imgtype );
			path = FileUtility.getPhotoDir(imgtype.getLoc(), img_.getName());
		}catch( Exception e ){
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String listPhoto(){
		
		List<Img> list = imgService.find("img", img);
		this.put("list", list);
		
		return SUCCESS;
	}
	
	public String toInsert(){
		List<Img> imgtypeList = imgService.find("img", null);
		this.put("imgtypeList", imgtypeList);
		return SUCCESS;
	}
	
	public String toUpdate(){
		img = imgService.findOne("img", img);
		
		List<Imgtype> imgtypeList = imgService.find("img", null);
		this.put("imgtypeList", imgtypeList);
		
		return SUCCESS;
	}
	
	public String update(){
		imgService.update("img", img);
		return SUCCESS;
	}

	public Img getImg() {
		return img;
	}

	public void setImg(Img img) {
		this.img = img;
	}

	public ImgService getImgService() {
		return imgService;
	}

	public void setImgService(ImgService imgService) {
		this.imgService = imgService;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}




}
